package theThirdWay_Peterson;

public class ThreadPop3 implements Runnable{
    private int a = 0;
    private int i=0;

    @Override
    public void run() {
        do{
            System.out.println(i+"ThreadPop Start:");
            MainClass.flag.set(a,true);
            MainClass.turn = 1;
            while (MainClass.flag.get(1)&&MainClass.turn == 1) {
//             busy waiting
            }
//        critical section start
//        打印栈顶元素
            if (!MainClass.stack.empty()) {
                System.out.println(i+"栈顶元素:" + MainClass.stack.peek()+"开始执行pop操作");
                MainClass.stack.pop();
            } else {
                System.out.println("空栈无法执行pop操作");
            }
//        critical section end,让出临界区
            MainClass.flag.set(a,false);
            i++;
        } while (i < 1000);

    }

}
